/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.magicland;

import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author eduardo
 */
public class GameManager {
    
    public static ArrayList<String> building;
    public static ArrayList<String> picking;
    private static Player player;
    
   // public static boolean isDragonfound;
   // private int dragonPosY;
    //private int dragonPosX;
    //private static String dragon;
    
    public MissionsManager missionManager;
    
    //Singleton
    public static GameManager gameManager;
    private GameManager(){
        
        missionManager = new MissionsManager();
    
    }
    public static GameManager getInstance(){
    
        if(gameManager == null)
            gameManager = new GameManager();
            
        return gameManager;
    }
            
    public void init(){
    
        building = new ArrayList<String>();
        building.add("str_select");
        building.add("str_house");
        building.add("str_plantation");
        building.add("str_store");   
        
       
        
        picking = new ArrayList<String>();
        
        /*
         Se adicionar um item coloque adicione uma regra no método pickingRules
         */
        picking.add("str_tree");
        picking.add("str_troll");
        picking.add("str_unicorn");
        picking.add("");//vazio
        picking.add("");//vazio

        
        //to-do: carregar configuracao inicial do player
          PlayerManager pm = new PlayerManager( );      
        try {
           player = pm.loadPlayer();
           player.getTutorialComplete();
            
        } catch (IOException ex) {
              ex.printStackTrace();
        } 
        
         Random random = new Random();
          
        //sorteia a posicao do dragao
        // int mapa 0 ou 1
         //this.setDragonPosY(random.nextInt(4));
         //this.setDragonPosX(random.nextInt(2));
        
    }

    
     public Player getPlayer(){
         return player;
     }
     
     public MissionsManager getMissionManager(){
         return missionManager;
     }
     
     public String pickingRules( String nomeItem ){
            
            
           System.out.println("nomeItem"+nomeItem+ " "+ Main.loc.getText( "str_dragon" ));
           String status="";
           if( Main.loc.getText( GameManager.picking.get(  0  ) ).equals(nomeItem))//tree
           {    //adiciona madeira
                player.setMadeira( player.getMadeira() + 5 ); 
                //adiciona Energia        
                player.setEnergia( player.getEnergia() - 1 ); 
                status = Main.loc.getText("str_wood") + " +5  "+Main.loc.getText("str_energy") + "  -1";
           }
           else if( Main.loc.getText( GameManager.picking.get(  1  ) ).equals(nomeItem))//troll
           {    
                //adiciona Energia
                player.setEnergia( player.getEnergia() - 1 ); 
                
                player.setOuro( player.getOuro() + 3 ); 
                status = Main.loc.getText("str_gold") + " +3  "+Main.loc.getText("str_energy") + "  -1";
           }
           else if( Main.loc.getText( GameManager.picking.get(  2  ) ).equals(nomeItem))//unicornion
           {
                player.setEnergia( player.getEnergia() + 5 ); 
                
                player.setOuro( player.getOuro() + 3 ); 
                status = Main.loc.getText("str_gold") + " +3  "+Main.loc.getText("str_energy") + "  +5";
           
           }
           
           else if( Main.loc.getText( "str_dragon" ).equals(nomeItem) )//Dragao
           {
                player.setEnergia( player.getEnergia() - 10 ); 
                
                player.setOuro( player.getOuro() + 15 ); 
               // status = Main.loc.getText("str_gold") + " +15  "+Main.loc.getText("str_energy") + "  -10";
                status = Main.loc.getText("str_congratulations_kill_dragon");
                Main.gui.setGameComplete();
                
           }
           return  status;
    }
    
        
    public boolean isPickedItem(String nomeItem ){
       
        System.out.println("nomeItem"+nomeItem);
        boolean isPickedItem = false;
        for(String idItem : picking){
             if( !"".equals(idItem) && Main.loc.getText(idItem).equals(nomeItem) ){
                 isPickedItem = true;
                 break; 
             }
        }
        //verfica se é dragao
        if(!isPickedItem && Main.loc.getText("str_dragon").equals(nomeItem) )
                 isPickedItem = true;
            
        
        return isPickedItem;
    }
    
    public void savePlayer(){
            
        PlayerManager pm = new PlayerManager( );      
        try {
             pm.savePlayer( GameManager.getInstance().getPlayer() );
            
        } catch (Exception ex) {
           ex.printStackTrace();
        } 
    
    }
    
    public void removeSave(){
        PlayerManager pm = new PlayerManager( ); 
        player = pm.removeSave();
        
        missionManager.resetMissions();
        
    }
    
    /*
     str_energy= Energy:
str_gold= Golden:
str_fruit= Fruit:
str_wood= Wood:
str_house= house
str_plantation= plantation
str_store= store
str_tree= tree
str_troll= troll
str_unicorn= unicorn
     */
    public String buildingRules( String nomeItem ){
            
           System.out.println("buildingItem"+nomeItem);
           String status="";
           if( Main.loc.getText( GameManager.building.get(  1  ) ).equals(nomeItem))//house
           {    //adiciona madeira
                player.setOuro( player.getOuro() - 1 ); 
                //adiciona Energia        
                player.setEnergia( player.getEnergia() - 1 ); 
                status = Main.loc.getText("str_gold") + " -1  "+Main.loc.getText("str_energy") + "  -1";
           }
           else if( Main.loc.getText( GameManager.building.get(  2  ) ).equals(nomeItem))//plantacao
           {    
                //adiciona Energia
                player.setEnergia( player.getEnergia() + 5 ); 
                
                player.setFruta( player.getFruta() + 3 ); 
                status = Main.loc.getText("str_energy") + " +5" +Main.loc.getText("str_fruit") + " +3";
           }
           else if( Main.loc.getText( GameManager.building.get(  3  ) ).equals(nomeItem))//store
           {
                player.setEnergia( player.getEnergia() + 3 ); 
                
                player.setOuro( player.getOuro() + 3 ); 
                status = Main.loc.getText("str_energy") + " +3" +Main.loc.getText("str_gold") + " +3";
           
           }
           return status;
    }

    /**
     * @return the dragonPosY
     */
//    public int getDragonPosY() {
//        return dragonPosY;
//    }

    /**
     * @param dragonPosY the dragonPosY to set
     */
//    public void setDragonPosY(int dragonPosY) {
//        this.dragonPosY = dragonPosY;
//    }

    /**
     * @return the dragonPosX
     */
//    public int getDragonPosX() {
//        return dragonPosX;
//    }

    /**
     * @param dragonPosX the dragonPosX to set
     */
//    public void setDragonPosX(int dragonPosX) {
//        this.dragonPosX = dragonPosX;
//    }  
}
